package znbkTopicType.resContent;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.example.lancooznbk.R;

import org.apache.commons.lang3.StringEscapeUtils;

import lancooControl.Control;
import znbkTopicType.WrapBaseAdapter;
import znbkTestPaperInfor.ZnbkPaperDef;

import static znbkTestPaperBj.activity.NotePaperActivity.mEntityBj;
import static znbkTestPaperRdtz.activity.HeatPointExtensionPaperActivity.mEntityRdtz;
import static znbkTestPaperSc.activity.CollectionPaperActivity.mEntitySc;

/**
 * Created by Administrator on 2018\3\18 0018.
 */

public class ZnbkContentResStlPtAdapter extends WrapBaseAdapter {

    private LayoutInflater mInflater;
    private Context mContext;
    //当前试题数量
    private int SumItem;
    //当前试题在当前试卷的ID
    private int PaperID;
    private int ModelFrom;//试卷来源


    private ViewHolder holder = null;
    private class ViewHolder{
        WebView WvTheme;//大题文本题干区

        private ViewHolder(View convertView) {
            WvTheme = (WebView) convertView.findViewById(R.id.wv_theme);
        }
    }

    /*
   * 获取小题选项信息
   * */
    public ZnbkContentResStlPtAdapter(Context context, int sumItem, int paperID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.SumItem = sumItem;
        this.PaperID = paperID;
        this.ModelFrom = modelFrom;
    }

    @Override
    public int getCount() {
        return SumItem;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.znbk_adapter_content_stl_pt_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /*
        * 获取作文题用户相关信息
        * */
        getQuesInfor();

        /*
        * 显示题干信息
        * */
        showQuesInfor();


        return convertView;
    }


    /*
    * 获取作文题用户相关信息
    * */
    private String GenreName = "";
    private String TypeName = "";
    private String QuesBody = "";
    private String subChildAsk;
    private int TotalItemCount = -1;

    private void  getQuesInfor(){

        if(ModelFrom== ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION) {


                /*
                * 小题数目
                * */
                    TotalItemCount = mEntityRdtz.getResFromTK().getTotalItemCount();

                /*
                * 题材类型：ResTypeName
                * */
                    GenreName = mEntityRdtz.getResFromTK().getGenreName();

                /*
                * 试题题型
                * */
                    TypeName = mEntityRdtz.getResFromTK().getTypeName();

                /*
                * 文本内容：词汇填空，改错题
                * */

                    if (TypeName.equals("词汇填空") || TypeName.equals("改错题")) {
                        if (GenreName.equals("选词填空")) {
                            //填空题中题材为短文填空
                            QuesBody = mEntityRdtz.getResFromTK().getQuesBody();
                        } else {
                            subChildAsk = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();
                            String arraySubChildAsk[] = new String[TotalItemCount + 1];
                            String subChildAskAll = "";
                            for (int i = 0; i < TotalItemCount + 1; i++) {
                                arraySubChildAsk[i] = Control.returnArray(subChildAsk, "____", 4)[i];
                                if (i < TotalItemCount) {
                                    subChildAskAll = subChildAskAll + arraySubChildAsk[i] + "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                                } else {
                                    subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                                }
                            }
                            subChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);
                        }
                    } else {

                /*
                * 文本内容：完型选择，阅读选择，阅读理解
                * */
                        QuesBody = mEntityRdtz.getResFromTK().getQuesBody();
                        if (TypeName.equals("完形选择")) {
                            String arrayQuesBody[] = new String[TotalItemCount + 1];
                            String quesBodyAll = "";
                            for (int i = 0; i < TotalItemCount + 1; i++) {
                                arrayQuesBody[i] = Control.returnArray(QuesBody, "____", 4)[i];
                                if (i < TotalItemCount) {
                                    quesBodyAll = quesBodyAll + arrayQuesBody[i] + "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                                } else {
                                    quesBodyAll = quesBodyAll + arrayQuesBody[i];
                                }
                            }
                            QuesBody = StringEscapeUtils.unescapeXml(quesBodyAll);
                        }
                    }

        }else if(ModelFrom==ZnbkPaperDef.FROM_COLLECTION){


                    /*
                    * 小题数目
                    * */
                    TotalItemCount = mEntitySc.getResDetail().getTotalItemCount();

                    /*
                    * 题材类型：ResTypeName
                    * */
                    GenreName = mEntitySc.getResDetail().getGenreName();

                    /*
                    * 试题题型
                    * */
                    TypeName = mEntitySc.getResDetail().getTypeName();

                    /*
                    * 文本内容：词汇填空，改错题
                    * */
                    if (TypeName.equals("词汇填空") || TypeName.equals("改错题")) {
                        if (GenreName.equals("选词填空")) {
                            //填空题中题材为短文填空
                            QuesBody = mEntitySc.getResDetail().getQuesBody();
                        } else {
                            subChildAsk = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();
                            String arraySubChildAsk[] = new String[TotalItemCount + 1];
                            String subChildAskAll = "";
                            for (int i = 0; i < TotalItemCount + 1; i++) {
                                arraySubChildAsk[i] = Control.returnArray(subChildAsk, "____", 4)[i];
                                if (i < TotalItemCount) {
                                    subChildAskAll = subChildAskAll + arraySubChildAsk[i] + "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                                } else {
                                    subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                                }
                            }
                            subChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);
                        }
                    } else {

                        /*
                        * 文本内容：完型选择，阅读选择，阅读理解
                        * */
                        QuesBody = mEntitySc.getResDetail().getQuesBody();
                        if (TypeName.equals("完形选择")) {
                            String arrayQuesBody[] = new String[TotalItemCount + 1];
                            String quesBodyAll = "";
                            for (int i = 0; i < TotalItemCount + 1; i++) {
                                arrayQuesBody[i] = Control.returnArray(QuesBody, "____", 4)[i];
                                if (i < TotalItemCount) {
                                    quesBodyAll = quesBodyAll + arrayQuesBody[i] + "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                                } else {
                                    quesBodyAll = quesBodyAll + arrayQuesBody[i];
                                }
                            }
                            QuesBody = StringEscapeUtils.unescapeXml(quesBodyAll);
                        }
                    }

        }else if(ModelFrom==ZnbkPaperDef.FROM_NOTE){


                    /*
                    * 小题数目
                    * */
            TotalItemCount = mEntityBj.getModelTKQue().getTotalItemCount();

                    /*
                    * 题材类型：ResTypeName
                    * */
            GenreName = mEntityBj.getModelTKQue().getGenreName();

                    /*
                    * 试题题型
                    * */
            TypeName = mEntityBj.getModelTKQue().getTypeName();

                    /*
                    * 文本内容：词汇填空，改错题
                    * */
            if (TypeName.equals("词汇填空") || TypeName.equals("改错题")) {
                if (GenreName.equals("选词填空")) {
                    //填空题中题材为短文填空
                    QuesBody = mEntityBj.getModelTKQue().getQuesBody();
                } else {
                    subChildAsk = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();
                    String arraySubChildAsk[] = new String[TotalItemCount + 1];
                    String subChildAskAll = "";
                    for (int i = 0; i < TotalItemCount + 1; i++) {
                        arraySubChildAsk[i] = Control.returnArray(subChildAsk, "____", 4)[i];
                        if (i < TotalItemCount) {
                            subChildAskAll = subChildAskAll + arraySubChildAsk[i] + "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                        } else {
                            subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                        }
                    }
                    subChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);
                }
            } else {

                        /*
                        * 文本内容：完型选择，阅读选择，阅读理解
                        * */
                QuesBody = mEntityBj.getModelTKQue().getQuesBody();
                if (TypeName.equals("完形选择")) {
                    String arrayQuesBody[] = new String[TotalItemCount + 1];
                    String quesBodyAll = "";
                    for (int i = 0; i < TotalItemCount + 1; i++) {
                        arrayQuesBody[i] = Control.returnArray(QuesBody, "____", 4)[i];
                        if (i < TotalItemCount) {
                            quesBodyAll = quesBodyAll + arrayQuesBody[i] + "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                        } else {
                            quesBodyAll = quesBodyAll + arrayQuesBody[i];
                        }
                    }
                    QuesBody = StringEscapeUtils.unescapeXml(quesBodyAll);
                }
            }

        }

    }

    /*
    * 显示界面信息
    * */
    private void showQuesInfor(){
        if(TypeName.equals("完形选择")||
                TypeName.equals("阅读填空")||
                TypeName.equals("阅读选择")||
                TypeName.equals("阅读理解")||
                TypeName.equals("词汇填空")||
                GenreName.equals("选词填空")||
                TypeName.equals("阅读简答")) {
            holder.WvTheme.loadDataWithBaseURL("about:blank", QuesBody, "text/html", "utf-8", null);
        }else{
            holder.WvTheme.loadDataWithBaseURL("about:blank", subChildAsk, "text/html", "utf-8", null);
        }
        holder.WvTheme.getSettings().setDefaultFontSize(18);
        holder.WvTheme.setFocusable(false);
    }


}